package beans;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class DAOCLass implements Serializable {

	private static final long serialVersionUID = 6171158984847946462L;

	public ArrayList<String> getData(String searchTerm, String searchOption){

		ArrayList<String> descList = new ArrayList<>();
		if(searchTerm != null)
		{
			if(searchOption.equals("0"))
			{
				try
				{
					Class.forName("com.mysql.jdbc.Driver");
					Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/totalitea", "root", "root");
					Statement stat= con.createStatement();
					ResultSet res= stat.executeQuery("SELECT * FROM product WHERE description LIKE \"%"+ searchTerm +"%\"");
	
	
	
					while(res.next())
					{
						descList.add(res.getString("description")+"/"+res.getString("id"));
					}
					res.close();
					stat.close();
					con.close();
				}
				catch(SQLException exception)
				{
					System.out.println(exception.getMessage());
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	
				return descList;
			}
			else
			{
				try
				{
					Class.forName("com.mysql.jdbc.Driver");
					Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/totalitea", "root", "root");
					Statement stat= con.createStatement();
					ResultSet res= stat.executeQuery("SELECT * FROM product WHERE category LIKE \"%"+ searchTerm +"%\"");
	
	
	
					while(res.next())
					{
						descList.add(res.getString("description")+"/"+res.getString("id"));
					}
					res.close();
					stat.close();
					con.close();
				}
				catch(SQLException exception)
				{
					System.out.println(exception.getMessage());
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	
				return descList;
	
			}
		}else
		{
			try
			{
				Class.forName("com.mysql.jdbc.Driver");
				Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/totalitea", "root", "root");
				Statement stat= con.createStatement();
				ResultSet res= stat.executeQuery("SELECT * FROM product");

				while(res.next())
				{
					descList.add(res.getString("description")+"/"+res.getString("id"));
				}
				res.close();
				stat.close();
				con.close();
				return descList;
			}
			catch(SQLException exception)
			{
				System.out.println(exception.getMessage());
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return descList;
		}
	}
	public double getWeight(ArrayList<String> id, ArrayList<String> amount)
	{
		double totalWeight = 0.0;
		double itemWeight = 0.0;
		for (String s : id)
		{
			try
			{
				Class.forName("com.mysql.jdbc.Driver");
				Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/totalitea", "root", "root");
				Statement stat = con.createStatement();
				ResultSet res = stat.executeQuery("SELECT itemWeight FROM product WHERE id = " + Integer.parseInt(s));
				itemWeight = res.getDouble("itemWeight");
				itemWeight = itemWeight * Double.parseDouble(amount.get(id.indexOf(s)));
				
			}
			catch(SQLException exception)
			{
				System.out.println(exception.getMessage());
			} 
			catch (ClassNotFoundException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			totalWeight += itemWeight;
		}
		return totalWeight;
	}
	public double getShipping(double totalWeight)
	{
		double shipping = 0.0;
		
		if (totalWeight > 5 && totalWeight < 10)
		{
			shipping = 10.0;
		}
		else if (totalWeight > 0 && totalWeight <= 5)
		{
			shipping = 1.0;
		}
		
		return shipping;
	}
}